<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['accident:basic:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['accident:basic:delete']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="selected.length === 0"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button
          v-hasPermi="['accident:basic:import']"
          type="success"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="$refs.file.click()"
        >导入</el-button> -->
        <input ref="file" type="file" style="display: none" @change="handleUpload">
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['accident:basic:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleDownload"
        >导入模板下载</el-button>
      </el-col> -->
    </el-row>

    <el-table
      ref="table"
      v-loading="loading"
      :data="accidentList"
      @selection-change="(selection) => selected = selection"
    >
      <el-table-column label="事件名称" prop="accName" />
      <el-table-column label="事件等级" prop="accLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_dict_problem_level" :value="scope.row.accLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="当前状态" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_task_handle_type" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="问题发生时间" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="结束时间" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['accident:basic:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['accident:basic:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="goDetailPage(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="事件名称" prop="accName">
          <el-input v-model="form.accName" placeholder="请输入事件名称" />
        </el-form-item>
        <el-form-item label="问题级别" prop="accLevel">
          <el-select v-model="form.accLevel" style="width: 240px">
            <el-option
              v-for="dict in dict.type.sys_dict_problem_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="当前状态" prop="status">
          <el-select v-model="form.status" style="width: 240px">
            <el-option
              v-for="dict in dict.type.sys_data_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="问题情况" prop="items">
          <accident-item-pars v-model="form.items" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccident, getAccident, delAccident, addAccident, updateAccident, importAccident } from '@/api/client/accident'
import AccidentItemPars from '../components/AccidentItemPars'

export default {
  name: 'AccidentList',
  components: { AccidentItemPars },
  dicts: ['sys_task_handle_type','sys_dict_problem_level'],
  props: {
    corpId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      loading: true,
      // 选中数组
      selected: [],
      accidentList: [],
      total: 0,

      // 弹出层标题
      title: '',
      open: false,

      // 查询参数
      queryParams: {
        corpId: this.corpId,
        pageNum: 1,
        pageSize: 10
      },

      // 表单参数
      form: {},
      rules: {
        accName: [
          { required: true, message: '事件名称不能为空', trigger: 'blur' }
        ],
        accLevel: [
          { required: true, message: '问题级别不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '当前状态不能为空', trigger: 'blur' }
        ],
        items: [
          { type: 'array', required: true, message: '问题情况不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listAccident(this.queryParams).then(response => {
        this.accidentList = response.data.records
        this.total = response.data.total
        this.loading = false
      }
      )
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加问题'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      getAccident(row.id).then(response => {
        this.form = {
          ...response.data
        }
        this.open = true
        this.title = '修改问题信息'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const formData = {
            id: '',
            ...this.form
          }
          if (this.form.id) {
            updateAccident(formData).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAccident(formData).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.selected.map(v => v.id)
      this.$modal.confirm(`确认删除 ${row.id ? row.name : '所选'} 数据？`).then(function() {
        return delAccident(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
        this.$refs.table.clearSelection()
        this.selected = []
      }).catch(() => {})
    },
    // 导入
    async handleUpload(event) {
      const data = new FormData()
      data.append('file', event.target.files[0])
      await importAccident(data)
      this.getList()
      this.$refs.file.value = null
    },
    // 导出过滤后的企业数据
    handleExport() {
      this.download('security/accidentInfo/export', this.queryParams, `问题基础表_${new Date().getTime()}.xlsx`)
    },
    // 下载模板
    handleDownload() {
      this.download('security/accidentInfo/importTemplate', {}, `问题基础表模板.xlsx`)
    },
    goDetailPage(row) {
      this.$router.push({
        name: 'ClientAccidentDetail',
        params: { certNo: this.cert_no || '1', id: row.id || '1' }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.busi-tag + .busi-tag {
  margin-left: 5px;
}
</style>
